package com.blog.service;

import com.blog.mapper.HrMapper;
import com.blog.mapper.MenuMapper;
import com.blog.mapper.MenuRoleMapper;
import com.blog.mapper.RoleMapper;
import com.blog.system.Hr;
import com.blog.system.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName BaseRoleService
 * @Destription
 * @Author 天堂小野花
 * @Date 2025/6/23  21:16
 * @Version 1.0
 */
@Service
public class BaseRoleService {
    @Autowired
    HrMapper hrMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    MenuRoleMapper menuRoleMapper;

    public List<Role> getAllRoles(){
        return roleMapper.getAllRoles();
    }


    public int addRole(Role role) {
        if(!role.getName().startsWith("ROLE_")){
            role.setName("ROLE_"+role.getName());
        }
        return roleMapper.insert(role);
    }

    public int deleteRoleById(Integer rid) {
        return roleMapper.deleteByPrimaryKey(rid);
    }

    public boolean updateMenuRole(Integer rid, Integer[] mids) {
        menuRoleMapper.deleteByRid(rid);
        if(mids==null || mids.length==0){
            return true;
        }
        Integer result =menuRoleMapper.insertRecord(rid,mids);
        return result == mids.length;
    }
}
